﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SzCourt.Logic.Common;
using NBearLite;
using SzCourt.Logic.NBL;
using KMFrame.Util;
using SzCourt.Logic.NBL.DBEntities;

namespace SzCourt.Logic.BLL
{
    public class AdminUser : SzCourtLogic
    {
        public bool CreateUser(string username, string password, string deptname)
        {
            bool result = false;
            string groupid = "";
            Admin_UserGroup adminUserGroup = NBLData.Select(SZCourtColumns.Admin_UserGroup)
               .Where(SZCourtColumns.Admin_UserGroup.GroupName == deptname)
               .ToSingleObject<Admin_UserGroup>();
            if (adminUserGroup == null)
                return false;
            groupid = adminUserGroup.ID.ToString();

            InsertSqlSection insert = NBLData.Insert(SZCourtColumns.Admin_User)
                .AddColumn(SZCourtColumns.Admin_User.memb_id, username)
                .AddColumn(SZCourtColumns.Admin_User.memb_pwd, SecurityHelper.MD5(password, 32))
                .AddColumn(SZCourtColumns.Admin_User.memb_group, groupid);
            int ri = insert.Execute();
            if (ri == 1)
                result = true;
            return result;
        }

        public bool EditPWD(string username, string oldPassword, string newPassword)
        {
            bool result = false;

            UpdateSqlSection update = NBLData.Update(SZCourtColumns.Admin_User)
                .AddColumn(SZCourtColumns.Admin_User.memb_pwd, SecurityHelper.MD5(newPassword, 32))
                .Where(SZCourtColumns.Admin_User.memb_id == username && SZCourtColumns.Admin_User.memb_pwd == SecurityHelper.MD5(oldPassword, 32));
                
            int count = update.Execute();
            if (count > 0)
                result = true;
            return result;
        }

        public AdminUserInfo AdminLogin(string username, string password)
        {
            AdminUserInfo result = new AdminUserInfo();
            ExpressionClip[] selectColumns =
           new ExpressionClip[] { 
                SZCourtColumns.Admin_User.memb_id, 
                SZCourtColumns.Admin_User.memb_group
            };
            Admin_User adminUserInfo = NBLData.Select(SZCourtColumns.Admin_User, selectColumns)
                .Where(SZCourtColumns.Admin_User.memb_id == username && SZCourtColumns.Admin_User.memb_pwd == SecurityHelper.MD5(password, 32))
                .ToSingleObject<Admin_User>();
            if (adminUserInfo == null)
                return null;
            result.UserName = adminUserInfo.memb_id;

            Admin_UserGroup adminUserGroup = NBLData.Select(SZCourtColumns.Admin_UserGroup)
                .Where(SZCourtColumns.Admin_UserGroup.ID.In(adminUserInfo.memb_group))
                .ToSingleObject<Admin_UserGroup>();
            if (adminUserGroup != null)
            {
                result.DeptName = adminUserGroup.GroupName;
            }
            return result;
        }
    }

    public class AdminUserInfo
    {
        public string UserName { get; set; }
        public string DeptName { get; set; }
    }
}
